import { HttpService } from '@nestjs/axios';
import { Injectable } from '@nestjs/common';
import { AxiosError } from 'axios';
import { catchError, firstValueFrom } from 'rxjs';
import { join } from 'node:path';
import { writeFileSync } from 'node:fs';

@Injectable()
export class MateService {
  constructor(private readonly httpService: HttpService) {}

  async bootstrap() {
    const { data } = await firstValueFrom(
      this.httpService
        .get<string>('https://mp.weixin.qq.com/s/d5bjBKTirWAW_7GC0TQ_wA', {
          headers: { Accept: 'application/json' },
        })
        .pipe(
          catchError((error: AxiosError) => {
            throw error.toJSON();
          }),
        ),
    );
    // console.info(data)
    const fileName = join(
      process.cwd(),
      'mate-storage',
      'd5bjBKTirWAW_7GC0TQ_wA.html',
    );
    console.info(`Write file to ${fileName}`);
    writeFileSync(fileName, data);
    return 'bootstrap';
  }
}
